package is.glif.fenius.webui.topo;

import is.glif.fenius.webui.common.WebUIException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class TopologyChecker {
    private static TopologyChecker instance;
    private TopologyChecker() {
        
    }
    public static TopologyChecker getInstance() {
        if (instance == null) {
            instance = new TopologyChecker();
        }
        return instance;
    }
    public void checkAll() throws JSONException, WebUIException {
        FileEdgeProvider ep = FileEdgeProvider.getInstance();
        FileNetworkProvider np = FileNetworkProvider.getInstance();
        ep.refresh();
        np.refresh();
        JSONArray networks = np.getNetworks("");
        for (int i = 0; i < networks.length(); i++) {
            JSONObject networkObj = networks.getJSONObject(i);
            String netId = networkObj.getString("id");
            JSONArray edges = ep.getEdges(netId, "", "");
            for (int j = 0; j < edges.length(); j++) {
                JSONObject edgeObj = edges.getJSONObject(j);
                String urn = (String) edgeObj.get("urn");
                System.err.println(urn);
                if (edgeObj.has("adjacencies")) {
                    JSONArray adjacencies = edgeObj.getJSONArray("adjacencies");
                    for (int k = 0; k < adjacencies.length(); k++) {
                        JSONObject adjData = adjacencies.getJSONObject(k);
                        String adjType = adjData.getString("type");
                        if (adjType.equals("network")) {
                            String adjNetId  = adjData.getString("peer");
                            String adjUrn = adjData.getString("remoteUrn");
                            // System.err.println(urn+ "   " +adjUrn);

                            JSONArray remoteNets = np.getNetworks(adjNetId);
                            for (int ii = 0; ii < remoteNets.length(); ii++) {
                                JSONArray remoteEdges = ep.getEdges(adjNetId, "", "");
                                for (int kk = 0 ; kk < remoteEdges.length(); kk++) {
                                    JSONObject remEdge = remoteEdges.getJSONObject(kk);
                                    String remUrn = remEdge.getString("urn");
                                    if (remEdge.has("adjacencies")) {
                                        JSONArray remAdjs  = remEdge.getJSONArray("adjacencies");
                                        for (int iii = 0; iii < remAdjs.length(); iii++) {
                                            JSONObject remAdjData = remAdjs.getJSONObject(iii);
                                            String remAdjType = remAdjData.getString("type");
                                            if (remAdjType.equals("network")) {
                                                String remAdjNetId  = remAdjData.getString("peer");
                                                String remAdjUrn = remAdjData.getString("remoteUrn");
                                                if (adjUrn.equals(remUrn)) {
                                                    if (!urn.equals(remAdjUrn)) {
                                                        System.err.println("check this link: \n"+
                                                        "    "+urn + " -> " +adjUrn+ " , but:\n " +
                                                        "    "+remUrn+ " -> "+remAdjUrn);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
